package tx.oopa.circuit.view;

import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.RenderingHints;
import java.awt.geom.Line2D;

import javax.swing.JComponent;

import tx.oopa.circuit.node.AbstractEdgingNode;
import tx.oopa.circuit.node.AbstractNode;

public class NodeLineDecorator extends NodeComponentDecorator {

    /**
     * Default UID
     */
    private static final long serialVersionUID = 1L;

    public NodeLineDecorator(JComponent component) {
        super(component);
    }

    @Override
    public void paint(Graphics g) {
        this.component.paint(g);
        for(AbstractNode node : MAP.keySet()) {
            Point start = MAP.get(node);
            if(start != null && node instanceof AbstractEdgingNode) {
                for(AbstractNode nextNode : ((AbstractEdgingNode) node)
                        .getEdgingNodes()) {
                    Point end = MAP.get(nextNode);
                    if(end != null) {
                        Graphics2D g2 = (Graphics2D) g;
                        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                                RenderingHints.VALUE_ANTIALIAS_ON);
                        g2.setPaint(Color.BLACK);
                        g2.draw(new Line2D.Double(this.correctPoint(start), this.correctPoint(end)));
                    }
                }
            }
        }
    }

    private Point correctPoint(Point p) {
        if(p == null) {
            throw new IllegalArgumentException("Point cannot be null");
        }
        Point correctPoint = new Point(p.x + NodeComponent.ACTUAL_WIDTH / 2, p.y
                + NodeComponent.ACTUAL_HEIGHT / 2);

        return correctPoint;
    }
}
